package jin.si.com.web;

import jin.si.com.entity.*;
import jin.si.com.service.LianXiService;
import jin.si.com.service.UserService;
import jin.si.com.utils.JsonData;
import jin.si.com.utils.TestDuanXin;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.util.*;

/**
 * @description:
 * @ClassName TestLianXiController
 * @Author MT
 * @Date 2021/5/26 19:10
 **/

@Controller
@RequestMapping("/Lianxi")
@PropertySource({"classpath:config.properties"})
public class LianXiController {

    @Autowired
    private LianXiService service;

    @Value("${path1}")
    private String path1;

    @RequestMapping("/yanzhengma")
    @ResponseBody
    public String yanzhengma(String phone){
        String show = TestDuanXin.show(phone);
        //System.out.println(show+"*********************************");
        return show;
    }

    @RequestMapping("/getUserName")
    @ResponseBody
    public List<LangFang> getUserName(){

        List<LangFang> list = service.getUserName();

        System.out.println(list+"****************************************");
        return list;

    }

    //旭日图
    @RequestMapping("/getXuRiTu")
    @ResponseBody
    public List<Map<String,Object>>  getXuRiTu() {
        List<Map<String,Object>>  list = service.getXuRiTu();
        return list;
    }


    //数据库用户信息导出Excel
    @RequestMapping("/getUsers")
    @ResponseBody
    public boolean getUsers() {
        List<Map<String,Object>> list = service.getUsers();


        try {
            FileOutputStream os=new FileOutputStream("d:/workboot/workboot2.xls");
            Workbook wb=new HSSFWorkbook();
            Sheet sheet = wb.createSheet();

            for(int a=0;a<list.size();a++){
                Map<String, Object> map = list.get(a);
                System.out.println(map+"***********");
                Row row = sheet.createRow(a);
                for(int b=0;b<map.size();b++){
                    Cell cell = row.createCell(0);
                    cell.setCellValue((String) map.get("name"));
                    Cell cell1 = row.createCell(1);
                    cell1.setCellValue((String) map.get("sex"));
                    Cell cell2 = row.createCell(2);
                    cell2.setCellValue((int) map.get("age"));
                    Cell cell3 = row.createCell(3);
                    cell3.setCellValue((String) map.get("pwd"));
                    Cell cell4 = row.createCell(4);
                    cell4.setCellValue((int) map.get("user_phone"));
                }
            }
            ((HSSFWorkbook) wb).write(os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }



        return true;
    }


    @RequestMapping("/getUser1")
    @ResponseBody
    public User getUser1(){
        User u=new User();
        u.setName("臭宝宝");
        u.setSex("男");
        u.setAge(20);
        System.out.println(u.getName()+"************************");
        return u;

    }

    @RequestMapping("/getUser2")
    @ResponseBody
    public User getUser2(int id){
        User u = service.getUser2(id);
        return u;
    }

    ///新闻
    @RequestMapping("/getNews")
    @ResponseBody
    public List<News> getNews() {
        List<News> news = service.getNews();
        return news;
    }

    //作品
    @RequestMapping("/getZuoPin")
    @ResponseBody
    public JsonData getZuoPin() {
        List<ZuoPin> list = service.getZuoPin();

        Map<String,Object> map=new HashMap<>();
        map.put("path1",path1);
        map.put("list",list);

        return  list!=null?JsonData.buildSuccess(map):JsonData.buildError("服务器出问题了");
    }

    //作品
    @RequestMapping("/getZuoPin2")
    @ResponseBody
    public JsonData getZuoPin2(int id) {

        ZuoPin zp = service.getZuoPin2(id);

        Map<String,Object> map=new HashMap<>();
        map.put("path1",path1);
        map.put("image",zp.getImage());
        map.put("createUser",zp.getCreateUser());
        map.put("like",zp.getLike());
        System.out.println(id+"*****************************");
        return  zp!=null?JsonData.buildSuccess(map):JsonData.buildError("服务器出问题了");
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException {
        /*String s1 = "Programming";
        String s2 = new String("Programming");
        String s3 = "Program";
        String s4 = "ming";
        String s6 = s3 + s4;
        String s5 = "Program" + "ming";

        System.out.println(s1 == s2);	//false
        System.out.println(s1 == s5);	//true
        System.out.println(s1 == s6);	//false
        System.out.println(s1 == s6.intern());	//true
        System.out.println(s2 == s2.intern());	//false

        short s=1;
        s= (short) (s+1);
        System.out.println(s);

        short s1=1;
        s1+=1;
        System.out.println(s1);

        Integer f1=100,f2=100,f3=150,f4=150,f5=-120,f6=-120,f7=-150,f8= -150,f9=-128,f10=-128,f11=127,f12=127;
        System.out.println(f1==f2);
        System.out.println(f3==f4);
        System.out.println(f5==f6);
        System.out.println(f7==f8);
        System.out.println(f9==f10);
        System.out.println(f11==f12);


        String str="12345";
        int a=Integer.parseInt(str);
        System.out.println(a);

        Double  d=Double.valueOf(str);
        System.out.println(d);

        String str2=d+"";
        System.out.println(str2);


        String str = "a";
        int a = str.compareTo("b");
        System.out.println(a);

        TreeSet t = new TreeSet();
        t.add(new Person(19, "小明"));
        t.add(new Person(18, "小强"));
        t.add(new Person(20, "小张"));

        Iterator iterator = t.iterator();
        while (iterator.hasNext()) {
            Person p = (Person) iterator.next();
            System.out.println(p);
        }*/


            /*TreeSet ts=new TreeSet();
            Pig p1=new Pig("狗",100);
            Pig p2=new Pig("猫",150);
            Pig p3=new Pig("老虎",500);

            ts.add(p1);
            ts.add(p2);
            ts.add(p3);

        Iterator iterator2 = ts.iterator();
        while (iterator2.hasNext()) {
            Pig p = (Pig) iterator2.next();
            System.out.println(p);
        }*/

        ObjectOutputStream objectOutputStream =new ObjectOutputStream(new FileOutputStream(new File("D://obj.txt")));
        objectOutputStream.writeObject(new Person(100,"老刘"));
        objectOutputStream.close();

        //对象输入流
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("D://obj.txt")));
        Person p = (Person)objectInputStream.readObject();
        System.out.println(p.getName()+"**********"+p.getAge());
        objectInputStream.close();
    }



    //轮播图
    @RequestMapping("/getCarousels")
    @ResponseBody
    public JsonData getCarousels() {
        List<Carousel> list = service.getCarousels();

        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("path",path1);
        return list!=null?JsonData.buildSuccess(map):JsonData.buildError("有错误");
    }

    //视频
    @RequestMapping("/getVideos")
    @ResponseBody
    public JsonData getVideos() {
        List<Video> list = service.getVideos();
        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("path",path1);
        return list!=null?JsonData.buildSuccess(map):JsonData.buildError("有错误");
    }
    
    @RequestMapping("/videoDatailApi")
    @ResponseBody
    public JsonData videoDatailApi(int id) {
        Video video = service.videoDatailApi(id);
        Map<String,Object> map=new HashMap<>();
        map.put("list",video);
        map.put("path",path1);

        return video!=null?JsonData.buildSuccess(map):JsonData.buildError("有错误");
    }



}
